﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Plugins;

namespace Xenta.Tests.Plugins
{
    [TestFixture]
    public class TwoCheckoutIPNHandlerTests : InfrastructureTest
    {
        [TestCase(10,
            "373125",
            "secret",
            "ORDER_CREATED",
            "bb24ee7a6662cccf194b43fba6cb3b64", 
            "373125", 
            "9e879f10-f91d-4749-ae3e-baf757b2def4", 
            "PENDING")]
        public void Can_HandleIPN(decimal amount,
            string sid,
            string secretWord,
            string message_type, 
            string md5_hash, 
            string vendor_id, 
            string vendor_order_id, 
            string invoice_status)
        {
            var ipn = new Dictionary<String, String>
            {
                {"message_type", message_type},
                {"md5_hash", md5_hash},
                {"vendor_id", vendor_id},
                {"vendor_order_id", vendor_order_id},
                {"invoice_status", invoice_status}
            };
            var handler = new TwoCheckoutPaymentProcessor();
            var guid = Guid.Empty;
            var status = PaymentFlags.None;

            string ret = handler.Identify(ipn, out guid);

            Assert.IsNullOrEmpty(ret);
            guid.AreEqual(Guid.Parse(vendor_order_id));
            
            var payment = new PaymentEntity
            {
                Invoice = new InvoiceEntity
                {
                    Order = new OrderEntity
                    {
                        Seller = new SellerEntity
                        {
                            Account = new AccountEntity
                            {
                                Attributes = new List<AccountAttributeEntity>
                                {
                                    new AccountAttributeEntity
                                    {
                                        Key = "Plugin.TwoCheckoutPaymentProcessor.SID",
                                        Value = sid
                                    },
                                    new AccountAttributeEntity
                                    {
                                        Key = "Plugin.TwoCheckoutPaymentProcessor.SecretWord",
                                        Value = secretWord
                                    }
                                }
                            }
                        }
                    }
                },
                Guid = guid,
                Amount = amount
            };
            ret = handler.Handle(ipn, payment, out status);

            Assert.IsNullOrEmpty(ret);
            status.AreEqual(PaymentFlags.Authorized);
        }
    }
}
